export const Guid = {
  NewGuid: function () {
    var guid = (
      this._G() +
      this._G() +
      '-' +
      this._G() +
      '-' +
      this._G() +
      '-' +
      this._G() +
      '-' +
      this._G() +
      this._G() +
      this._G()
    ).toLowerCase()
    return guid
  },
  _G: function () {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)
  }
}
// 时间转换成年-月-日
export const formateDateYMD = parms => {
  if(parms==null||parms==undefined||parms.length<1){
    return ''
  }
  function addDateZero (num) {
    return num < 10 ? '0' + num : num
  }
  const d = new Date(parms)
  const formatdatetime =
    d.getFullYear() +
    '-' +
    addDateZero(d.getMonth() + 1) +
    '-' +
    addDateZero(d.getDate())
  return formatdatetime
}
// 时间转换成年.月.日
export const formateDateYearSpot = parms => {
  if(parms==null||parms==undefined){
    return ''
  }
  function addDateZero (num) {
    return num < 10 ? '0' + num : num
  }
  const d = new Date(parms)
  const formatdatetime =
    d.getFullYear() +
    '.' +
    addDateZero(d.getMonth() + 1) +
    '.' +
    addDateZero(d.getDate())
  return formatdatetime
}
// 获取当前年份
export const formateDateY = parms => {
  if(parms==null||parms==undefined){
    return ''
  }
  const d = new Date(parms)
  const formatdateDay = d.getFullYear() 
  return formatdateDay
}
// 时间转换成XX年XXX月XXX日
export const formateDateYYMMDD = parms => {
  if(parms==null||parms==undefined){
    return ''
  }
  function addDateZero (num) {
    return num < 10 ? '0' + num : num
  }
  const d = new Date(parms)
  const formatdatetime =
    d.getFullYear() +
    '年' +
    addDateZero(d.getMonth() + 1) +
    '月' +
    addDateZero(d.getDate()) + '日'
  return formatdatetime
}
// 时间转换成XXXXXXXX(20201001)
export const formateDateyNumber = parms => {
  if(parms==null||parms==undefined){
    return ''
  }
  function addDateZero (num) {
    return num < 10 ? '0' + num : num
  }
  const d = new Date(parms)
  const formatdatetime =
    d.getFullYear() +
    addDateZero(d.getMonth() + 1) +
    addDateZero(d.getDate())
  return formatdatetime
}
// 时间间隔天
export const intervalDay = (sDate1, sDate2) => {    //sDate1和sDate2是2006-12-18格式 
  let dateSpan;
  let iDays;
  sDate1 = Date.parse(sDate1);
  sDate2 = Date.parse(sDate2);
  dateSpan = sDate2 - sDate1;
  if (dateSpan<0) {
    return 0
  }
  iDays = Math.floor(dateSpan / (24 * 3600 * 1000));
  return iDays
}
// 判断文件是否可以被转换成pdf 或者文件是图片 视频类型
// (是否可预览)
export const previewFlage = DocumentPath => {
  if (DocumentPath != null && DocumentPath != undefined) {
    const index = DocumentPath.lastIndexOf('.')
    // 获取文件类型
    const suffix = DocumentPath.substr(index + 1)
    const typeList = [
      'png',
      'jpg',
      'jpeg',
      'bmp',
      'gif',
      'webp',
      'psd',
      'svg',
      'tiff',
      'mp4',
      'avi',
      'rmvb',
      'bmp',
      'mov',
      'rm',
      'flv',
      'svg',
      '3gp',
      'pdf',
      'xlsx',
      'xls',
      'docx',
      'doc',
      'ppt',
      'pptx',
      'tif',
      'pdf',
      'txt',
      'jfif'
    ]
    if (typeList.indexOf(suffix.toLowerCase()) !== -1) {
      return true // k可预览
    } else {
      return false
    }
  } else {
    return false
  }
}
// 去掉路径中的盘符
export const deleteDriveLetter = DocumentPath => {
  if (DocumentPath != null && DocumentPath != undefined) {
    return DocumentPath.slice(2, DocumentPath.length)
  } else {
    return false
  }
}
// 获取文件后缀 
export const getFileSuffix = fileName => {
  const index= fileName.lastIndexOf(".")
  const suffix = fileName.substr(index+1)
  return suffix
}
// 判断文件类型
export const fileType = fileName => {
  const index= fileName.lastIndexOf(".")
  const suffix = fileName.substr(index+1)
  if (
    ['png', 'jpg', 'jpeg', 'bmp', 'gif', 'webp', 'psd', 'svg', 'tiff', 'jfif'].indexOf(
      suffix.toLowerCase()
    ) !== -1
  ) {
    return 'img' // 图片类型
  } else if (
    ['mp4', 'avi', 'rmvb', 'bmp', 'mov', 'rm', 'flv', 'svg', '3gp'].indexOf(
      suffix.toLowerCase()
    ) !== -1
  ) {
    return 'video' // 视频
  } else if (['pdf'].indexOf(suffix.toLowerCase()) !== -1) {
    return 'pdf' // paf可转化成pdf
  } else if (
    ['xlsx', 'xls', 'xlsm', 'xltx', 'xlam'].indexOf(
      suffix.toLowerCase()
    ) !== -1
  ) {
    return 'xls' // 表格
  }else if (
    ['doc', 'docx', 'dotx', 'dotm'].indexOf(
      suffix.toLowerCase()
    ) !== -1
  ) {
    return 'doc' // word文档
  }else if (
    ['pptx', 'pptm', 'ppsx', 'potx', 'potm', 'ppam'].indexOf(
      suffix.toLowerCase()
    ) !== -1
  ) {
    return 'ppt' // word文档
  }else {
    return 'unknown'
  }
}
// 获取文件名称
export const fileName = fileName => {
  if( fileName == null || fileName == undefined) {
    return ''
  }
  const index= fileName.lastIndexOf("/")
  const filename = fileName.substr(index+1)
  return filename
}
// 获取路由参数
export const routerParams = () => {
  const locationHref = window.location.href
  const index = locationHref.indexOf('?')
  const routerParam = locationHref.substr(index + 1)
  const vars = routerParam.split('&')
  const search = []
  for (let i = 0; i < vars.length; i++) {
    const pair = vars[i].split('=')
    search.push({ key: pair[0], value: pair[1] })
  }
  let routerParms = {}
  search.forEach(item=> {
    routerParms[item.key] = item.value
  })
  // 以对象的形式返回所有请求参数
  return routerParms
}
